
import os, unittest
from databrain_db import Database

class DatabaseFile(unittest.TestCase):

    def testDBfile(self):
        # TODO: need a different path for Win32
        self.filename = '/tmp/test_db'
        self.assertEqual(os.path.exists(self.filename), False)
        self.test_db = Database(self.filename)
        self.assertEqual(os.path.exists(self.filename), True)
        os.remove(self.filename)
        self.assertEqual(os.path.exists(self.filename), False)

class NewDatabase(unittest.TestCase):

    def setUp(self):
        self.test_db = Database(':memory:')

    def testInvalidDatum(self):
        result = self.test_db.query_data_id('invalid')
        invalid_id = result.fetchone()[0]
        self.assertEqual(invalid_id, 1)
        result = self.test_db.query_data(invalid_id)
        self.assertEqual(result.fetchone()[0], 'invalid')

    def testTagDatum(self):
        result = self.test_db.query_data_id('tag')
        tag_id = result.fetchone()[0]
        self.assertEqual(tag_id, 2)
        result = self.test_db.query_data(tag_id)
        self.assertEqual(result.fetchone()[0], 'tag')

    def testSearch(self):
        result = self.test_db.query_search_data('inva')
        data = result.fetchone()
        self.assertEqual(data[0], 1)
        self.assertEqual(data[1], 'invalid')

    def testRecent(self):
        result = self.test_db.query_recent_data()
        self.assertEqual(result.fetchone(), None)

    def testQueryDataTypes(self):
        result = self.test_db.query_data_types(1)
        self.assertEqual(result.fetchone(), None)

    def testQueryDataMembers(self):
        result = self.test_db.query_data_members(1)
        self.assertEqual(result.fetchone(), None)

    def testAllTags(self):
        result = self.test_db.query_all_tags()

if __name__ == "__main__":
    unittest.main()

